package com.caisejingyu.whales.home.devices.printer;

import android.util.Pair;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

/**
 * Created by guazi on 16-9-19.
 */
public class Shell {

    static public String exec(String cmd) {
        Process process = null;
        OutputStreamWriter os = null;
        DataInputStream is = null;
        StringBuilder log = new StringBuilder();
        try {
            process = Runtime.getRuntime().exec("sh");
            os = new OutputStreamWriter(process.getOutputStream());
            is = new DataInputStream(process.getInputStream());

            //os.write("\n");
            os.write(cmd);
            os.write("\n");
            os.write("exit");
            os.write("\n");
            os.flush();

            try {
                process.waitFor();
                String line;
                while ((line = is.readLine()) != null) {
                    log.append(line + "\n");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (process != null) {
                process.destroy();
            }
        }
        return log.toString();
    }

    static public String execSu(String cmd) {
        Process process = null;
        StringBuilder log = new StringBuilder();
        OutputStreamWriter os = null;
        DataInputStream is = null;
        try {
            process = Runtime.getRuntime().exec("su");
            os = new OutputStreamWriter(process.getOutputStream());
            is = new DataInputStream(process.getInputStream());

            //os.write("\n");
            os.write(cmd);
            os.write("\n");
            os.write("exit");
            os.write("\n");
            os.flush();

            try {
                process.waitFor();
                String line;
                while ((line = is.readLine()) != null) {
                    log.append(line + "\n");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (process != null) {
                process.destroy();
            }
        }
        return log.toString();
    }


    static public Pair<Integer, String> execSuExit(String cmd) {
        Process process = null;
        StringBuilder log = new StringBuilder();
        OutputStreamWriter os = null;
        DataInputStream is = null;
        String msg = "";
        try {
            process = Runtime.getRuntime().exec("su");
            os = new OutputStreamWriter(process.getOutputStream());
            is = new DataInputStream(process.getInputStream());

            //os.write("\n");
            os.write(cmd);
            os.write("\n");
            os.write("exit");
            os.write("\n");
            os.flush();

            try {
                process.waitFor();
                String line;
                while ((line = is.readLine()) != null) {
                    log.append(line + "\n");
                }
                System.out.println(process.exitValue());
                if (process.exitValue() == 0) {
                    return new Pair<>(1, "执行成功");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                msg = e.getMessage();
            }
        } catch (IOException e) {
            e.printStackTrace();
            msg = e.getMessage();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    msg = e.getMessage();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    msg = e.getMessage();
                }
            }
            if (process != null) {
                process.destroy();
            }
        }
        return new Pair<>(2, msg);
    }
}
